import tkinter as tk
from tkinter import ttk
import get_data as gd
import tkinter.font as tf
import draw_picture as dp



def init_window():
    window = tk.Tk()
    window.geometry("650x400")
    window.title("疫情实时数据查询系统")
    window.resizable(0,0)

    l1 = tk.Label(window,width=33,height = 1,text = "疫情实时数据查询系统",fg = "white",bg = "#0f52da",font = tf.Font(family = "华文隶书",size = 30))
    l1.grid(row = 0,column = 0,columnspan = 2)
    nb = ttk.Notebook(window,width=630,height = 320)

    ttk.Style().configure(".", font=("仿宋", 12)) 



    return window,nb

def insert_tab_one(data1):
    nb = data1[1]
    window = data1[0]
    tab1 = tk.Frame(nb)
    nb.add(tab1,text = "国内疫情")
    tab2 = tk.Frame(nb)
    nb.add(tab2,text = "国外疫情")
    nb.grid(row = 1,column = 0)
    return window, tab1, tab2


def insert_tab_two(data2):
    window = data2[0]
    tab1 = data2[1]
    nb2 = ttk.Notebook(tab1,width = 600,height = 280)
    tab3 = tk.Frame(nb2,width=10)
    nb2.add(tab3,text = "疫情整体情况")
    tab4 = tk.Frame(nb2)
    nb2.add(tab4, text = "各地区疫情情况")
    tab5= tk.Frame(nb2)
    nb2.add(tab5, text = "北京市疫情情况")
    nb2.grid(row=0, column = 0)

    return tab3, tab4, tab5, window


def insert_foreign(data2):
    tab2 = data2[2]
    nb3 = ttk.Notebook(tab2,width=600,height = 280)
    tab6 = tk.Frame(nb3, width=10)
    nb3.add(tab6,text = "国外疫情总体情况")
    tab7 = tk.Frame(nb3)
    nb3.add(tab7,text = "国外各国疫情情况")
    nb3.grid(row=0,column=0)

    return tab6, tab7


  


def insert_texts(data3):
    window = data3[3]
    tab3 = data3[0]
    tab4 = data3[1]
    data1 = gd.read_data1()
    font = tf.Font(family = "华文仿宋",size=12)

    t1 = tk.Text(tab3,width=35,height=1,font=font)
    t1.grid(row=0,column=0)
    t1.insert("end","统计截至时间："+str(data1['lastUpdateTime']))

    t2 = tk.Text(tab3,width=35,height=1,font=font)
    t2.grid(row=0,column=1)
    t2.insert("end","数据来源：腾讯实时疫情")

    t3 = tk.Text(tab3,width=35,height=1,font=font,foreground="red")
    t3.grid(row=1,column=0)
    t3.insert("end","全国累计确诊人数："+str(data1['chinaTotal']['confirm']))

    t4 = tk.Text(tab3,width=35,height=1,font=font,foreground="red")
    t4.grid(row=1,column=1)
    t4.insert("end","全国现存确诊人数："+str(data1['chinaTotal']['nowConfirm']))

    t5 = tk.Text(tab3,width=35,height=1,font=font,foreground="#e8b004")
    t5.grid(row=2,column=0)
    t5.insert("end","全国疑似病例："+str(data1['chinaTotal']['suspect']))

    t6 = tk.Text(tab3,width=35,height=1,font=font,foreground="#e8b004")
    t6.grid(row=2,column=1)
    t6.insert("end","相较于昨天新增疑似人数："+str(data1['chinaAdd']['suspect']))

    t7 = tk.Text(tab3,width=35,height=1,font=font,foreground="green")
    t7.grid(row=3,column=0)
    t7.insert("end","全国治愈人数："+str(data1['chinaTotal']['heal']))

    t8 = tk.Text(tab3,width=35,height=1,font=font,foreground="green")
    t8.grid(row=3,column=1)
    t8.insert("end","相较于昨天新增治愈人数："+str(data1['chinaAdd']['heal']))

    t9 = tk.Text(tab3,width=35,height=1,font=font,foreground="gray")
    t9.grid(row=4,column=0)
    t9.insert("end","全国死亡人数："+str(data1['chinaTotal']['dead']))

    t10 = tk.Text(tab3,width=35,height=1,font=font,foreground="gray")
    t10.grid(row=4,column=1)
    t10.insert("end","相较于昨天新增死亡人数："+str(data1['chinaAdd']['dead']))
    f1 = tk.Frame(tab3,borderwidth = 1,width = 570,height = 100,bd = 10)
    f1.grid(row=5, column=0,columnspan = 2)

    b1 = tk.Button(f1,width=28,text = "疫情每日新增图",font = tf.Font(family = "华文仿宋",size=12))
    b1.grid(row=0,column=0)

    b2 = tk.Button(f1,width=28,text = "疫情当前确诊图",font = tf.Font(family = "华文仿宋",size=12))
    b2.grid(row=0,column=1)

    b3 = tk.Button(f1,width=28,text ="疫情累计图", font = tf.Font(family = "华文仿宋",size=12))
    b3.grid(row=1,column=0)

    b4 = tk.Button(f1,width=28,text ="全国治愈死亡率图", font = tf.Font(family = "华文仿宋",size=12))
    b4.grid(row=1,column=1)

    c1 = tk.Canvas(f1,height=20)
    c1.grid(row=2,column = 0,columnspan=2)
    t11 = tk.Text(f1,width= 56, font = tf.Font(family = "华文仿宋",size=12),height = 3,bg="#f0f0f0",relief = "flat")
    t11.grid(row = 3,column = 0,columnspan = 2)
    t11.insert("end","        疫情仍在发展，腾讯的数据接口和北京市卫生健康委员会的网页也在不断改变。所以在接口改变后程序可能无法正常使用，不过我已经尽最大可能避免这种情况发生，希望大家理解。")

    t12=tk.Text(tab4,width=52,height=0.6,font=font,foreground="black")
    t12.grid(row = 0, column = 0)
    t12.insert("end","   "+"请在后方蓝框输入您要查询的省份（或直辖市）如：北京")

    e1=tk.Entry(tab4,width=17,bg = "#c7e9f2")
    e1.grid(row=0, column = 1)

    b5 = tk.Button(tab4,text ="查看",font=tf.Font(family = "华文仿宋",size = 12),width = 5,height = 1)
    b5.grid(row = 0,column = 2)

    f2 = tk.Frame(tab4)
    f2.grid(row=2,column=0, columnspan=3)
    scrollBar = tk.Scrollbar(f2)

    tree = ttk.Treeview(f2,show='headings',yscrollcommand=scrollBar.set)

    

    return window, b1, b2, b3, b4, t6, e1, b5, scrollBar, tree


def insert_text_two(data4):
    tab6 = data4[0]
    tab7 = data4[1]
    data1 = gd.read_data1()
    data2 = gd.read_data4()
    font = tf.Font(family = "华文仿宋",size=12)

    tt1 = tk.Text(tab6,width=35,height=1,font=font)
    tt1.grid(row=0,column=0)
    tt1.insert("end","统计截至时间："+str(data1['lastUpdateTime']))

    tt2 = tk.Text(tab6,width=35,height=1,font=font)
    tt2.grid(row=0,column=1)
    tt2.insert("end","数据来源：腾讯疫情(数据均除中国外)")

    tt3 = tk.Text(tab6,width=35,height=1,font=font,foreground="red")
    tt3.grid(row=1,column=0)
    tt3.insert("end","全球累计确诊人数："+str(data2['globalStatis']['confirm']))

    tt4 = tk.Text(tab6,width=35,height=1,font=font,foreground="red")
    tt4.grid(row=1,column=1)
    tt4.insert("end","相较于昨天新增确诊人数："+str(data2['globalStatis']['confirmAdd']))

    tt5 = tk.Text(tab6,width=35,height=1,font=font,foreground="#e8b004")
    tt5.grid(row=2,column=0)
    tt5.insert("end","全球现存确诊病例："+str(data2['globalStatis']['nowConfirm']))

    tt6 = tk.Text(tab6,width=35,height=1,font=font,foreground="#e8b004")
    tt6.grid(row=2,column=1)
    tt6.insert("end","相较于昨天新增现存确诊人数："+str(data2['globalStatis']['nowConfirmAdd']))

    tt7 = tk.Text(tab6,width=35,height=1,font=font,foreground="green")
    tt7.grid(row=3,column=0)
    tt7.insert("end","全球治愈人数："+str(data2['globalStatis']['heal']))

    tt8 = tk.Text(tab6,width=35,height=1,font=font,foreground="green")
    tt8.grid(row=3,column=1)
    tt8.insert("end","相较于昨天新增治愈人数："+str(data2['globalStatis']['healAdd']))

    tt9 = tk.Text(tab6,width=35,height=1,font=font,foreground="gray")
    tt9.grid(row=4,column=0)
    tt9.insert("end","全球死亡人数："+str(data2['globalStatis']['dead']))

    tt10 = tk.Text(tab6,width=35,height=1,font=font,foreground="gray")
    tt10.grid(row=4,column=1)
    tt10.insert("end","相较于昨天新增死亡人数："+str(data2['globalStatis']['deadAdd']))
    ff1 = tk.Frame(tab6,borderwidth = 1,width = 570,height = 100,bd = 10)
    ff1.grid(row=5, column=0,columnspan = 2)

    bb1 = tk.Button(ff1,width=28,text = "国外疫情累计图",font = tf.Font(family = "华文仿宋",size=12))
    bb1.grid(row=0,column=0)

    bb2 = tk.Button(ff1,width=28,text = "国外治愈死亡率图",font = tf.Font(family = "华文仿宋",size=12))
    bb2.grid(row=0,column=1)

    ff2 = tk.Frame(tab7)
    ff2.grid(row=2,column=0, columnspan=3)
    scrollBar2 = tk.Scrollbar(ff2)

    tree2 = ttk.Treeview(ff2,show='headings',yscrollcommand=scrollBar2.set)

    return bb1, bb2, scrollBar2, tree2

def delForm(tree):
    x=tree.get_children()
    for item in x:
        tree.delete(item)

def write_in_flag():
    with open("control.txt",'w') as file_obj:
        file_obj.write("0")


def insert_tree_one(event,m,m2):
    font = tf.Font(family = "华文仿宋",size=12)
    e1 = m2[6]
    scrollBar = m2[8]
    tree = m2[9]
    scrollBar.pack(side=tk.RIGHT,fill = tk.Y)
    ttk.Style().configure("Treeview", font=("微软雅黑", 9))



    with open('control.txt',"r") as file_object:
        contents = file_object.read()
    if int(contents) == 1:
        delForm(tree)
    data1 = gd.read_data1()['areaTree'][0]['children']
    path = str(e1.get())
    path_data = []
    path_china = []
    for i in data1:
        path_china.append(i['name'])
        path_data.append(i['children'])
    if path in path_china:
        num = path_china.index(path)
        data_path = path_data[num]

        ttk.Treeview.column(tree,"#0",width=100,anchor="center")
        tree["columns"] = ('地区','累计确诊人数','相较于昨日新增确诊人数','累计治愈人数','治愈率','累计死亡人数','死亡率') 
        tree.column('地区', width=100)
        tree.column('累计确诊人数', width=90)
        tree.column('相较于昨日新增确诊人数', width=85)
        tree.column('累计治愈人数', width=90)
        tree.column('治愈率', width=60)
        tree.column('累计死亡人数', width=90)
        tree.column('死亡率', width=60)

        tree.heading('地区', text='地区')
        tree.heading('累计确诊人数', text='累计确诊人数') 
        tree.heading('相较于昨日新增确诊人数', text='今日确诊') 
        tree.heading('累计治愈人数', text='累计治愈人数') 
        tree.heading('治愈率', text='治愈率') 
        tree.heading('累计死亡人数', text='累计死亡人数') 
        tree.heading('死亡率', text='死亡率')
        
        h=0
        for i in data_path:
            name = i['name']
            today = i['today']
            total = i['total']
            tree.insert("",h, text = name,values=(name,str(total['confirm']), str(today['confirm']), str(total['heal']), str(total['healRate']), str(total['dead']), str(total['deadRate'])))
            h+=1

        with open("control.txt",'w') as file_obj:
            file_obj.write("1")
        tree.pack()
        scrollBar.config(command=tree.yview)


def insert_tree_three(data5):
    font = tf.Font(family = "华文仿宋",size=12)
    scrollBar2 = data5[2]
    tree2 = data5[3]
    scrollBar2.pack(side=tk.RIGHT,fill = tk.Y)

    ttk.Style().configure("Treeview", font=("微软雅黑", 9))

    with open('control.txt',"r") as file_object:
        contents = file_object.read()

    if int(contents) == 1:
        delForm(tree2)
    data2 = gd.read_data4()['foreignList']
    path_data = []
    path_world = []

    ttk.Treeview.column(tree2,"#0",width=100,anchor="center")
    tree2["columns"] = ('国家','累计确诊人数','相较于昨日新增确诊人数','现存确诊人数','相较于昨日新增现存确诊','累计治愈人数','相较于昨日新增治愈','累计死亡人数','相较于昨日新增死亡') 
    tree2.column('国家', width=80)
    tree2.column('累计确诊人数', width=60)
    tree2.column('相较于昨日新增确诊人数', width=60)
    tree2.column('现存确诊人数', width=60)
    tree2.column('相较于昨日新增现存确诊', width=80)
    tree2.column('累计治愈人数', width=60)
    tree2.column('相较于昨日新增治愈', width=60)
    tree2.column('累计死亡人数', width=60)
    tree2.column('相较于昨日新增死亡', width=60)

    tree2.heading('国家', text="国家")
    tree2.heading('累计确诊人数', text='累计确诊') 
    tree2.heading('相较于昨日新增确诊人数', text='新增确诊') 
    tree2.heading('现存确诊人数', text='现存确诊') 
    tree2.heading('相较于昨日新增现存确诊', text='新增现存确诊') 
    tree2.heading('累计治愈人数', text='累计治愈') 
    tree2.heading('相较于昨日新增治愈', text='新增治愈') 
    tree2.heading('累计死亡人数', text='累计死亡') 
    tree2.heading('相较于昨日新增死亡', text='新增死亡')
        
    h=0
    for i in data2:
        name=i['name']
        tree2.insert("",h,values=(name,str(i['confirm']), str(i['confirmAdd']), str(i["nowConfirm"]),str(i["nowConfirmCompare"]),str(i['heal']), str(i['healCompare']), str(i['dead']), str(i['deadCompare'])))
        h+=1

    with open("control.txt",'w') as file_obj:
        file_obj.write("1")
    tree2.pack()
    scrollBar2.config(command=tree2.yview)
  



def insert_second_window():
    datas3 = gd.read_data3()
    datas1 = datas3["list"][0]["city"]
    for data1 in datas1:
        name = data1["name"]
        if str(name) == "石景山区":
            data = int(data1["zerodays"])
    
    window2 = tk.Toplevel()
    window2.geometry("400x150")
    window2.title("开学建议")
    ft = tf.Font(family = "华文仿宋",size=15)
    t1 = tk.Text(window2,font=ft)
    t1.pack()

    if data <=14:
        t1.insert("end","        不建议开学，因为石景山区14日内有增长。建议学生居家学习。")
       
    elif data >14 and data < 28 :
        
        t1.insert("end","        不太建议开学，因为石景山区"+str(data)+"天内有增长。WHO建议，连续两个最长潜伏期无增长才可放心。建议学生居家学习。")
    elif data >= 28:
       
        t1.insert("end","        可以开学。因为石景山区已经连续"+str(data)+"天无增长。WHO建议，连续两个最长潜伏期无增长才可放心。不过开学与北京市的整体情况有关，要视北京市的情况而定。")

    window2.mainloop()
    
def insert_tree_two(data3):
    tab5 = data3[2]
    ttk.Style().configure("Treeview", font=("微软雅黑", 9))
    ft = tf.Font(family = "华文仿宋",size=11)
    b6 = tk.Button(tab5,width=23,text = "北京市确诊病例男女比例",command = dp.draw_pie_picture_one,font = ft)
    b6.grid(row=0,column=0)
    b7 = tk.Button(tab5,width=23,text = "北京市确诊病例年龄比例",command = dp.draw_pie_picture_two,font = ft)
    b7.grid(row=0,column=1)
    b8 = tk.Button(tab5,width = 23,text="石景山区是否开学建议",command = insert_second_window,font = ft)
    b8.grid(row = 0,column=2)
    

    columns = ("row", "data", "time")
    tree = ttk.Treeview(tab5, show = "headings", columns = columns, selectmode = tk.BROWSE)
    tree.column("row", anchor = "center",width = 10)
    tree.column("data", anchor = "center",width = 480)
    tree.column("time", anchor = "center",width = 105)
    tree.heading("row", text = "")
    tree.heading("data", text = "北京市疫情信息实时更新(信息来源：北京市卫健委)")
    tree.heading("time", text = "时间")

    lists = gd.get_news()
    lists1 = lists[0]
    lists2 = lists[1]
    i=0
    for list1 in lists1:
        tree.insert("",i,values = (str(i+1),list1,lists2[i]))
        i+=1
    tree.grid(row=1,column=0,columnspan = 3)


def handlerAdaptor(fun, **kwds):
    return lambda event,fun=fun,kwds=kwds: fun(event, **kwds)
    
def bind_buttons(buttons,data3,data5):
    window = buttons[0]
    b1 = buttons[1]
    b2 = buttons[2]
    b3 = buttons[3]
    b4 = buttons[4]
    b5 = buttons[7]
    bb1 = data5[0]
    bb2 = data5[1]
    bb5 = data5[2]
    b1.bind("<Button-1>", dp.draw_daily_add_line)
    b2.bind("<Button-1>", dp.draw_now_confirm_line)
    b3.bind("<Button-1>", dp.draw_country_sum_line)
    b4.bind("<Button-1>", dp.draw_rate_line)
    b5.bind("<Button-1>",handlerAdaptor(insert_tree_one, m=data3, m2=buttons))
    bb1.bind("<Button-1>", dp.draw_foreign_country)
    bb2.bind("<Button-1>", dp.draw_foreign_rate)
    window.mainloop()